/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gridworld_v5;

import java.awt.FlowLayout;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author Jonah
 */
public class Game extends JFrame{
    /*implementation comment*/
    ////<editor-fold defaultstate="collapsed" desc="Instanced Variables">
    private Board board;
    private ThreadedUserSettings tus;
    private UserSettingJFrame usjf;
    private List<ThreadedActor> actors;
    private boolean pause = false, running = false;
    private ActorDirector ad;
    public PossibleStates placingState;
    public GameVariables gv;
    
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Constructor">
    public Game() {
        gv = new GameVariables();
        board = new Board( gv.pixels, gv.Rows, gv.Columns, this);
        tus = new ThreadedUserSettings();
        usjf = new UserSettingJFrame();
        usjf.setGame(this);
        placingState = PossibleStates.Empty;
        actors = new LinkedList();
        ad = new ActorDirector(gv,this);
        
        setLayout(new FlowLayout());
        add(board);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        pack();
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Class Methods">
    public ActorDirector getActorDirector(){
        return ad;
    }
    public void removeActor(ThreadedActor actor){
        actors.remove(actor);
    }
    void processTileClicked(Tile tile) {
        //these should have been written as a switch statement
        if ( placingState.equals(PossibleStates.Bug)){
            ThreadedBug bug = new ThreadedBug( this, board, tile);
            actors.add(bug);
            bug.moveToTile(tile);
            if(running) new Thread(bug).start();
        }
        if ( placingState.equals(PossibleStates.Critter)){
            ThreadedCritter crit = new ThreadedCritter( this , board, tile);
            actors.add(crit);
            crit.moveToTile(tile);
            if(running) new Thread(crit).start();
        }
        if ( placingState.equals(PossibleStates.Flower)){
            ThreadedFlower flower = new ThreadedFlower( this , board, tile);
            actors.add(flower);
            flower.moveToTile(tile);
            if(running) new Thread(flower).start();
        }
        if ( placingState.equals(PossibleStates.Rock)){
            ThreadedRock rock = new ThreadedRock( this , board, tile);
            actors.add(rock);
            rock.moveToTile(tile);
//          if(running) new Thread(bug).start();
        }
    }
    public void updateGameVariables(){
        gv.Bugs = 0;
        gv.Critters = 0;
        gv.Flowers = 0;
        gv.Rocks = 0;
        for (ThreadedActor ta: actors){
            if (ta.isTypeOf(PossibleStates.Bug)){
                gv.Bugs++;
            }
            if (ta.isTypeOf(PossibleStates.Flower)){
                gv.Flowers++;
            }
            if (ta.isTypeOf(PossibleStates.Critter)){
                gv.Critters++;
            }
        }
    }
    public void startSimulation() {
        if ( !running )
        {
            
            if (!pause){
                updateGameVariables();
                new Thread(ad).start();
                for (ThreadedActor actor : actors){
                    new Thread(actor).start();
                }
            }
            running = true;
            return;
        }
        if (running) {
            if (!pause) System.out.println("simulation already running");
            if (pause){
                pause = false;
                for (ThreadedActor actor : actors){
                    actor.unpause();
                }
            }
        }
    }
    
    public void pauseSimulation(){
        //System.out.println("pause actors:" + actors);
        pause = true;
//        for (ThreadedActor actor : actors){
//            actor.pause();
//        }
    }
    public List<ThreadedActor> getActors(){
        return actors;
    }
    //</editor-fold>
}
